package com.xandone.wcdog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xandone.wcdog.mapper.JokeMapper;
import com.xandone.wcdog.mapper.UserMapper;
import com.xandone.wcdog.mapper.VideoMapper;
import com.xandone.wcdog.pojo.Base.BaseListResult;
import com.xandone.wcdog.pojo.*;
import com.xandone.wcdog.service.JokeService;
import com.xandone.wcdog.service.VideoService;
import com.xandone.wcdog.utils.IDUtils;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ：xandone
 * created on  ：2019/1/15 22:30
 * description：
 */
@Service
public class VideoServiceIml implements VideoService {
    @Autowired
    VideoMapper videoMapper;
    @Autowired
    UserMapper userMapper;


    public VideoBean addVideo(Map<String, String> map) throws Exception {
        VideoBean videoBean = new VideoBean();

        videoBean.setVideoId(IDUtils.RandomId());
        videoBean.setVideoUserId(map.get("videoUserId"));
        videoBean.setTitle(map.get("title"));
        videoBean.setContent(map.get("content"));
        videoBean.setContentHtml(map.get("contentHtml"));
        videoBean.setPostTime(new Date());
        videoBean.setArtCommentCount(0);
        videoBean.setArtLikeCount(0);
        videoBean.setCategory(map.get("category"));
        videoBean.setTags(0);
        videoBean.setCoverImg(map.get("coverImg"));
        videoMapper.addVideo(videoBean);

        UserBean userBean = userMapper.getUserById(map.get("videoUserId"));
        videoBean.setVideoUserNick(userBean.getNickname());
        videoBean.setVideoUserIcon(userBean.getUserIcon());
        return videoBean;
    }


    @Override
    public BaseListResult getAllVideo(Integer page, Integer row, Integer tag) throws Exception {
        BaseListResult base = new BaseListResult();
        PageHelper.startPage(page, row);
        List<VideoBean> list = videoMapper.getVideoListTags(tag);
        int total = (int) new PageInfo<>(list).getTotal();
        base.setData(list);
        base.setTotal(total);
        return base;
    }

    @Override
    public void deleteVideoById(String videoId) throws Exception {
        videoMapper.deleteVideoById(videoId);
    }

    @Override
    public void deleteVideoByList(List<String> videoIds) throws Exception {
        videoMapper.deleteVideoByList(videoIds);
        videoMapper.deleteVideoCommentByVideoIdList(videoIds);
    }

    @Override
    public void addVideoComment(VideoCommentBean bean) throws Exception {
        videoMapper.addVideoComment(bean);
    }

    @Override
    public BaseListResult getAllVideoCommentById(Integer page, Integer row, String videoId) throws Exception {
        BaseListResult base = new BaseListResult();
        PageHelper.startPage(page, row);
        List<VideoCommentBean> list = videoMapper.getVideoCommentById(videoId);
        int total = (int) new PageInfo<>(list).getTotal();
        for (VideoCommentBean bean : list) {
            UserBean user = userMapper.getUserById(bean.getCommentUserId());
            if (user != null) {
                bean.setCommentNick(user.getNickname());
                bean.setCommentIcon(user.getUserIcon());
            }
        }

        base.setData(list);
        base.setTotal(total);
        return base;
    }

    @Override
    public void deleteCommentList(List<String> commentsId) throws Exception {
        videoMapper.deleteVideoCommentList(commentsId);
    }

    @Override
    public void deleteCommentByVideoId(String videoId) throws Exception {
        videoMapper.deleteVideoCommentByVideoId(videoId);
    }

    public void thumbsVideo(String videoId, String userId) throws Exception {
        VideoLikeBean videoLikeBean = new VideoLikeBean(videoId, userId, new Date());
        videoMapper.thumbsVideo(videoLikeBean);
    }

    public void collectsVideo(String videoId, String userId) throws Exception {
        VideoCollectBean videoCollectBean = new VideoCollectBean(videoId, userId, new Date());
        videoMapper.collectsVideo(videoCollectBean);
    }



    public List<VideoLikeBean> selectVideoLikeById(String videoId) throws Exception {
        return videoMapper.selectVideoLikeById(videoId);
    }

    @Override
    public void changeVideoLikeCount(Map<String, Object> map) throws Exception {
        videoMapper.changeVideoLikeCount(map);
    }

    @Override
    public void changeVideoCollectCount(Map<String, Object> map) throws Exception {
        videoMapper.changeVideoCollectCount(map);
    }

    @Override
    public BaseListResult getUserSelfVideos(Integer page, Integer row, String userId) throws Exception {
        BaseListResult base = new BaseListResult();
        PageHelper.startPage(page, row);
        List<VideoBean> list;
        if (TextUtils.isEmpty(userId) || "-1".equals(userId)) {
            list = new ArrayList<>();
        } else {
            list = videoMapper.getUserSelfVideos(userId);
        }
        int total = (int) new PageInfo<>(list).getTotal();

        for (VideoBean bean : list) {
            dealVideoBean(bean);
        }

        base.setData(list);
        base.setTotal(total);
        return base;
    }

    @Override
    public BaseListResult getVideoLikeByUserId(Integer page, Integer row, String userId) throws Exception {
        BaseListResult base = new BaseListResult();
        PageHelper.startPage(page, row);
        List<VideoBean> datas = new ArrayList<>();
        List<VideoLikeBean> list = videoMapper.selectVideoLikeByUserId(userId);
        for (VideoLikeBean bean : list) {
            VideoBean videoBean = videoMapper.getVideoBeanById(bean.getVideoId());
            videoBean.setSelfApprovalTime(bean.getApprovalTime());
            dealVideoBean(videoBean);
            datas.add(videoBean);
        }
        int total = (int) new PageInfo<>(list).getTotal();
        base.setData(datas);
        base.setTotal(total);
        return base;
    }

    @Override
    public BaseListResult getVideoCollectByUserId(Integer page, Integer row, String userId) throws Exception {
        BaseListResult base = new BaseListResult();
        PageHelper.startPage(page, row);
        List<VideoBean> datas = new ArrayList<>();
        List<VideoCollectBean> list = videoMapper.selectVideoCollectByUserId(userId);
        for (VideoCollectBean bean : list) {
            VideoBean videoBean = videoMapper.getVideoBeanById(bean.getVideoId());
            videoBean.setSelfApprovalTime(bean.getApprovalTime());
            dealVideoBean(videoBean);
            datas.add(videoBean);
        }
        int total = (int) new PageInfo<>(list).getTotal();
        base.setData(datas);
        base.setTotal(total);
        return base;
    }

    private VideoBean dealVideoBean(VideoBean bean) throws Exception {
        UserBean user = userMapper.getUserById(bean.getVideoUserId());
        List<VideoLikeBean> likeBeans = selectVideoLikeById(bean.getVideoId());
        List<VideoCommentBean> commentBeans = videoMapper.getVideoCommentById(bean.getVideoId());
        if (user != null) {
            bean.setVideoUserNick(user.getNickname());
            bean.setVideoUserIcon(user.getUserIcon());
        }
        if (likeBeans != null) {
            bean.setArtLikeCount(likeBeans.size());
        }
        if (commentBeans != null) {
            bean.setArtCommentCount(commentBeans.size());
        }
        return bean;
    }

    @Override
    public VideoBean getVideoById(String videoId) throws Exception {
        VideoBean videoBean = videoMapper.getVideoBeanById(videoId);
        dealVideoBean(videoBean);
        return videoBean;
    }

    @Override
    public BaseListResult getVideoListFog(Integer page, Integer row, String key) throws Exception {
        BaseListResult base = new BaseListResult();
        PageHelper.startPage(page, row);
        List<VideoBean> list;
        key = URLDecoder.decode(key, "utf-8");
        list = videoMapper.getVideoListFog(key);
        int total = (int) new PageInfo<>(list).getTotal();

        for (VideoBean bean : list) {
            dealVideoBean(bean);
        }

        base.setData(list);
        base.setTotal(total);
        return base;
    }

    @Override
    public BaseListResult searchVideoList(Integer page, Integer row, VideoBean videoBean) throws Exception {
        BaseListResult base = new BaseListResult();
        PageHelper.startPage(page, row);
        List<VideoBean> list = videoMapper.searchVideoList(videoBean);
        int total = (int) new PageInfo<>(list).getTotal();

        for (VideoBean bean : list) {
            dealVideoBean(bean);
        }

        base.setData(list);
        base.setTotal(total);
        return base;
    }
}
